﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Threading.Tasks;
using WTAppletProjectHTTPS.Models;

namespace WTAppletProjectHTTPS.Controllers
{
    [JwtAuthentication]
    public class FrameStationChangeMaterialConfirmationController : ApiController
    {
        private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
        // POST api/<controller>
        public async Task<HttpResponseMessage> Post([FromBody] FrameStationChangeMaterialConfirmation FSCMC)
        {
            ReturnObject RO = new ReturnObject();
            if (FSCMC == null ||
                 string.IsNullOrEmpty(FSCMC.FSCMC_OperatorConfirmation) ||
                 string.IsNullOrEmpty(FSCMC.FSCMC_LineParting) ||
                 string.IsNullOrEmpty(FSCMC.FSCMC_ProductModel) ||
                 string.IsNullOrEmpty(FSCMC.FSCMC_FrameLOGOPosition) ||
                 string.IsNullOrEmpty(FSCMC.FSCMC_FrameColor) ||
                 string.IsNullOrEmpty(FSCMC.FSCMC_ButtonLOGOPosition) ||
                 string.IsNullOrEmpty(FSCMC.FSCMC_ButtonColor) ||
                 string.IsNullOrEmpty(FSCMC.FSCMC_FluorescentParticleColor))
            {
                Logger.Warn("输入值无效");
                RO.Status = HttpStatusCode.BadRequest;
                RO.Message = "输入值无效";
                RO.Data = null;
                return Request.CreateResponse(HttpStatusCode.BadRequest, RO);
            }

            using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionName"].ConnectionString))
            {
                connection.Open();
                using (var transaction = connection.BeginTransaction())
                {
                    try
                    {
                        var query = @"INSERT INTO FrameStationChangeMaterialConfirmation 
                                    (FSCMCOperatorConfirmation, FSCMCConfirmationTime, FSCMCProductModel, FSCMCLineParting, 
                                     FSCMCFrameLOGOPosition, FSCMCFrameColor, FSCMCButtonLOGOPosition, 
                                     FSCMCButtonColor, FSCMCFluorescentParticleColor, FSCMCNonconformanceDescriptionIfAny, FSCMCNonconformanceImage,FSCMCCreateCreator,FSCMCCreateTime)
                                    VALUES 
                                    (@OperatorConfirmation, @ConfirmationTime, @ProductModel, @LineParting, 
                                     @FrameLOGOPosition, @FrameColor, @ButtonLOGOPosition, 
                                     @ButtonColor, @FluorescentParticleColor, @NonconformanceDescriptionIfAny, @NonconformanceImage,@CreateCreator,@CreateTime);
                                    SELECT CAST(scope_identity() AS int);";

                        SqlParameter[] parameters = {
                            new SqlParameter("@OperatorConfirmation", SqlDbType.NVarChar, 50) { Value = FSCMC.FSCMC_OperatorConfirmation },
                            new SqlParameter("@ConfirmationTime", SqlDbType.DateTime) { Value = FSCMC.FSCMC_ConfirmationTime },
                            new SqlParameter("@ProductModel", SqlDbType.NVarChar, 50) { Value = FSCMC.FSCMC_ProductModel },
                            new SqlParameter("@LineParting", SqlDbType.NVarChar, 50) { Value = FSCMC.FSCMC_LineParting },
                            new SqlParameter("@FrameLOGOPosition", SqlDbType.NVarChar, 50) { Value = FSCMC.FSCMC_FrameLOGOPosition },
                            new SqlParameter("@FrameColor", SqlDbType.NVarChar, 50) { Value = FSCMC.FSCMC_FrameColor },
                            new SqlParameter("@ButtonLOGOPosition", SqlDbType.NVarChar, 50) { Value = FSCMC.FSCMC_ButtonLOGOPosition },
                            new SqlParameter("@ButtonColor", SqlDbType.NVarChar, 50) { Value = FSCMC.FSCMC_ButtonColor },
                            new SqlParameter("@FluorescentParticleColor", SqlDbType.NVarChar, 50) { Value = FSCMC.FSCMC_FluorescentParticleColor },
                            new SqlParameter("@NonconformanceDescriptionIfAny", SqlDbType.NVarChar, 500) { Value = (object)FSCMC.FSCMC_NonconformanceDescriptionIfAny ?? DBNull.Value },
                            new SqlParameter("@NonconformanceImage", SqlDbType.NVarChar, 1000) { Value = (object)FSCMC.FSCMC_NonconformanceImage ?? DBNull.Value },
                            new SqlParameter("@CreateCreator", SqlDbType.NVarChar, 50) { Value = FSCMC.FSCMC_CreateCreator },
                            new SqlParameter("@CreateTime", SqlDbType.DateTime) { Value = DateTime.Now },
                        };

                        using (var command = new SqlCommand(query, connection, transaction))
                        {
                            command.Parameters.AddRange(parameters);
                            var result = await command.ExecuteScalarAsync();
                            if (result != null)
                            {
                                transaction.Commit();
                                FSCMC.FSCMC_ID = Convert.ToInt32(result);
                                Logger.Info("新增成功");
                                RO.Status = HttpStatusCode.OK;
                                RO.Message = "OK";
                                RO.Data = FSCMC;
                                return Request.CreateResponse(HttpStatusCode.OK, RO);
                            }
                            else
                            {
                                transaction.Rollback();
                                Logger.Error("插入记录失败");
                                RO.Status = HttpStatusCode.InternalServerError;
                                RO.Message = "Error";
                                RO.Data = null;
                                return Request.CreateResponse(HttpStatusCode.InternalServerError, RO);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        Logger.Warn("新增失败: " + ex.Message);
                        RO.Status = HttpStatusCode.InternalServerError;
                        RO.Message = "Error" + ex.Message;
                        RO.Data = null;
                        return Request.CreateResponse(HttpStatusCode.InternalServerError, RO);
                    }
                }
            }
        }
    }
}